{include 'header.tpl'}
<div class="contant">
    <div class="header">
        <div class="header_s">
            <div class="search">
                <div class="region">长沙市<i></i></div>
                <div class="sr_region"><input type="search" id="search" placeholder="请输入您的收货地址" class=""></div>
            </div>
        </div>
    </div>

    <form name="myForm">
        <input id="string_x" type="hidden" name="myHidden" value="{$string_x}"/>
        <input id="string_y" type="hidden" name="province" value="{$string_y}"/>
    </form>

    <div id="allmap" style="width: 100%;height: 300px;"></div>

    <div class="loc">
        <div class="loc_tlt">附近地址</div>
        <div class="loc_list">
            <ul id="wordMap"></ul>
        </div>
    </div>
</div>
<script type="application/javascript" src="/mall/js/lib.js"></script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="application/javascript" src='http://api.map.baidu.com/api?v=2.0&ak=pRtqXqnajTytAzWDL3HOnPRK'></script>

{literal}

    <script>
        //delCookie('_base');
        //获取当前位置
        function getQueryString(queryname) {
            var qKeys = { };
            var re = /[?&]([^=]+)(?:=([^&]*))?/g;
            var matchInfo;
            while (matchInfo = re.exec(location.search)) {
                qKeys[matchInfo[1]] = matchInfo[2];
            }
            return typeof (qKeys[queryname]) == 'undefined' ? '0' : qKeys[queryname];
        }
        var id=getQueryString('id');
        var nick=getQueryString('nick');
        var mobile=getQueryString('mobile');
        var sex=getQueryString('sex');
        var housemember=getQueryString('housemember');
        var string_x=$('#string_x').val();
        var string_y=$('#string_y').val();
    //        alert(string_x);
    //        alert(string_y);


        var map = new BMap.Map("allmap");
        map.centerAndZoom(new BMap.Point(string_x,string_y),16);
        map.enableScrollWheelZoom(true);

        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){

            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
//                alert('您的位置：'+r.point.lng+','+r.point.lat);
                getPois(r.point.lng,r.point.lat);
            }
            else {
                alert('failed'+this.getStatus());
            }
        },{enableHighAccuracy: true});

        var isQuery = true;
        var mapRes = { };
        var mapOpt = { };

        var local = new BMap.LocalSearch('长沙', {
            pageCapacity: 20,
            onSearchComplete: function(result){
                isQuery = true;
                $("#wordMap li").remove();
                var arr = [];
                if (local.getStatus() == BMAP_STATUS_SUCCESS) {
//                    console.log(result);
                    for (var i = 0; i < result.getCurrentNumPois(); i++) {
                        var item = result.getPoi(i);
                        if (!item.title || (item.tags && !item.tags.length) || !item.province || !item.address || !item.point.lng || /市$/.test(item.title) || /省$/.test(item.title) || /^朝阳区$/.test(item.title)) {
                            continue
                        }
                        var isTag = true;
                        item.tags = item.tags ? item.tags : [];
                        for (var j = 0; j < item.tags.length; j++) {
                            var tag = item.tags[j];
                            if (tag == '区县' || tag == '行政地标' || tag == '地级市' || tag == '名称标注类' || tag == '公交车站') {
                                isTag = false;
                                break
                            }
                        }
                        if (!isTag) {
                            continue;
                        }
                        var list = $("<li><a href='javascript:;'>"+item.title+"</a></li>");
                        list.data("lat", item.point.lat);
                        list.data("lng", item.point.lng);
                        list.data("city", result.city);
                        list.data("province", result.province);
                        list.data("address", item.address);
                        list.data("title", item.title);
                        // 关键字加红色
                        var re = new RegExp(result.keyword);
                        item.title = item.title.replace(re, '<b>' + result.keyword + '</b>');

                        arr.push(list[0]);
                    }
                    // 排序
                    arr = fieldsSort(arr, '+dis', "");
                    for (var x in arr) {
                        var items = arr[x];
                        $(items).appendTo($("#wordMap"));
                    }
                }
            }
        });

        function fail() {
            alert('定位失败');
        }

            // 定义周边POI检索类
            var POI = new BMap.Geocoder();

            function getPois(x,y){
                //var point = new BMap.Point(112.89211,28.214494);
                var point = new BMap.Point(x,y);
                POI.getLocation(point,function(res){
//                    console.log(res);
                    $("#wordMap li").remove();
                    var arr = [];
                    for(var i in res.surroundingPois){
                        var item = res.surroundingPois[i];
                        if (!item.title || !item.address) {
                            continue;
                        }
                        var list = $("<li><a href='javascript:;'>"+item.title+"</a></li>");
                        list.data("lng", item.point.lng);
                        list.data('lat', item.point.lat);
                        list.data("latlog", item.point.lng + ',' + item.point.lat);
                        list.data("city", res.addressComponents.city);
                        list.data("province", res.addressComponents.province);
                        list.data("address", res.address);
                        list.data("district", res.addressComponents.district);
                        list.data("title", item.title);
                        arr.push(list[0]);
                    }
                    // 排序
//                    console.log(list[0]);
//                    console.log(res.address);
                    arr = fieldsSort(arr, '+dis', "");
                    for (var x in arr) {
                        var items = arr[x];
                        $(items).appendTo($("#wordMap"))
                    }
                },{
                    poiRadius : 3000,
                    numPois : 10
                });
            }


            // 搜索框 联想搜索
            $("#search").on("input",function() {
                if(!isQuery || $(this).val() == "" ) return;
                $("#wordMap li").remove();
                isQuery = false;
                local.search($("#search").val().trim(),{forceLocal: true});
            });

            // 搜索框聚焦
            $(document).on('focus','#search',function(){
                $(this).trigger("input");
            });

            $(document).on('click','#wordMap li',function () {
//                base.zb = $(this).data('latlog');
//                var diqu = $(this).data('address');
                var    lng=$(this).data('lng');
                var    lat=$(this).data('lat');
                var  province =$(this).data('province');
                var city =$(this).data('city');
                var    county= $(this).data('district') || '';
//                base.province =$(this).data('province');
                var  detail = $(this).data('title');

//                setCookie('_base',base);
//                console.log(province);
//                console.log(city);
//                console.log(county);
//                console.log(detail);
                location.href = '/wwwmall/add/getBaiduAddress?id='+id+'&province='+province+'&city='+city+'&county='+county+'&detail='+detail+'&nick='+nick+'&mobile='+mobile+'&sex='+sex+'&housemember='+housemember+'&lng='+lng+'&lat='+lat;
            });


        // 计算距离
        function count(lat_a, lng_a, lat_b, lng_b) {
            var pk = 180 / 3.14169;
            var a1 = lat_a / pk;
            var a2 = lng_a / pk;
            var b1 = lat_b / pk;
            var b2 = lng_b / pk;
            var t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
            var t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
            var t3 = Math.sin(a1) * Math.sin(b1);
            var tt = Math.acos(t1 + t2 + t3);
            return 6366000 * tt
        }
        // 微信分享
        var wx_config = {
            tag: '定位'
        };

        // 排序
        var fieldsSort = function(arr,field1,field2) {
            var char1=field1.charAt(0),
                    char2=field1.charAt(0),
                    dir1=char1==="-"?-1:1,
                    dir2=char2==="-"?-1:1;
            if(char1==="-"||char1==="+")field1=field1.substr(1);
            if(char2==="-"||char2==="+")field2=field2.substr(1);
            arr.sort(function(a,b){
                return a[field1]===b[field1]?(a[field2]-b[field2])*dir2:(a[field1]-b[field1])*dir1;
            });
            return arr;
        };

            //红点定位 获取经纬度
        map.addEventListener("click",function(e){
//        alert(e.point.lng + "," + e.point.lat);
            getPois(e.point.lng ,e.point.lat);
            map.clearOverlays();
            var new_point = new BMap.Point(e.point.lng,e.point.lat);
            var marker = new BMap.Marker(new_point);  // 创建标注
            map.addOverlay(marker);              // 将标注添加到地图中
            map.panTo(new_point);


            //以下是获取当前的地理位置
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function(r){
                if(this.getStatus() == BMAP_STATUS_SUCCESS){
                    //表示获取成功那么 r 这个参数就包含有当前的地理位置经纬度
                    //逆地址解析，就是要把当前的经纬度转为当前具体地理位置
                    //逆地址解析
                    var geoc = new BMap.Geocoder();
                    var pt = new BMap.Point(e.point.lng,  e.point.lat);//实例化这两个点
                    geoc.getLocation(pt, function (rs) {

                        var addComp = rs.addressComponents;
//                        $('.its-place').html(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber)
//                        alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);

                        //对应的省市区、县街道，街道号
                    });

                }else {
                    alert('failed'+this.getStatus());
                }
            },{enableHighAccuracy : true});
        });

    </script>
{/literal}
{include 'footer.tpl'}